export const createApiRouter = (data: any) => {
    data = Array.isArray(data) ? data : [data];
    const modules = import.meta.glob("../views/**");
    return (
        data &&
        data.map((e: any) => {
            const name = e.name;
            const path = e.path;
            const alias = e.alias;
            const disabled = e.disabled;
            const redirect = e.children[0] && e.children[0].path;
            const ancestors = e.ancestors || null;
            let ffs =
                e.ancestors &&
                e.ancestors
                    .map((es: any, i: any) => {
                        let url = "";
                        if (i != e.ancestors.length - 1) {
                            url += es + "/";
                        } else {
                            url += es + ".vue";
                        }
                        return url;
                    })
                    .join("");
            const component =
                e.ancestors && e.ancestors.length > 0
                    ? modules[`../views/${ffs}`]
                    : null;

            let children = [] as any;

            if (e.children && e.children.length > 0) {
                children = createApiRouter(e.children);
            }

            if (e.ancestors) {
                return {
                    name,
                    path,
                    ancestors,
                    alias, 
                    redirect,
                    component,
                    disabled,
                    ...(children && children.length > 0 && { children }),
                };
            } else {
                return {
                    name,
                    path, 
                    redirect,
                    alias,
                    ancestors,
                    disabled,
                    ...(children && children.length > 0 && { children }),
                };
            }
        })
    );
};

